﻿using System.Collections.Generic;
using System.ComponentModel.Composition;
using InvestmentIntelligence.Files;
using InvestmentIntelligence.SchedulingService.Infrastructure;
using InvestmentIntelligence.TradingData.DataProcessing.DataResolvers;
using InvestmentIntelligence.TradingData.Handlers.Handlers.CsvHandlers;
using InvestmentIntelligence.TradingData.Handlers.Mus.FieldsConfiguration;
using InvestmentIntelligence.TradingData.Handlers.PostHandlersProcessing;
using InvestmentIntelligence.TradingData.Handlers.PostHandlersProcessing.Capital;
using InvestmentIntelligence.TradingData.TradingDataModels;

namespace InvestmentIntelligence.TradingData.Handlers.Mus
{
    [Export(typeof(IScheduledTaskFactory))]
    [ExportMetadata("TaskName", "LoadMusCsvCapital")]
    public class CsvMusCapitalHandlerFactory : FolderBasedHandlerFactoryBase, IScheduledTaskFactory
    {
        private readonly ScheduledTaskParameter _capitalFolder = new StringScheduledTaskParameter(
            "capital-dir", "specifies a directory containing capital data");

        private readonly ScheduledTaskParameter _fileMask = new StringScheduledTaskParameter(
         "files-mask", "specifies a mask of position files");

        private readonly ScheduledTaskParameter _dateMask = new StringScheduledTaskParameter(
           "date-mask", "specifies a mask of date field");

        public override IEnumerable<ScheduledTaskParameter> GetParameters()
        {
            yield return _capitalFolder;
            yield return _fileMask;
            yield return _dateMask;
        }

        public override IScheduledTask CreateTask(CreateTaskParams createTaskParams, IDictionary<ScheduledTaskParameter, string> arguments)
        {
            var fileds = new CapitalFields();
            var files = FilteredFileReader.GetFiles(_capitalFolder.Get(arguments, ""), _fileMask.Get(arguments, ""), "csv");
            var dateMask = _dateMask.Get(arguments, "M/d/yyyy");
            var postProcessing = new List<PostProcessing<TradingDataCapitalDto>>
                {
                    new GeneralCalculateOpenCapitalPostProcessing()
                };

            return new CsvImportFundbookCapitalsHandler(files, fileds, Mappings.TradingDataCapitalDto, new []{dateMask}, new FundBookResolver(), postProcessing);
        }
    }
}